<template>
  <div class="app-container">
    <el-button style="margin-bottom: 20px;" type="primary" @click="add(0)">添加角色</el-button>
    <el-form :inline="true" style="float:right;" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input v-model="keyWord.keyWord" placeholder="请输入昵称/账户名搜索" />
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="getList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableData.data"
      stripe
      style="width: 100%"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        prop="id"
        label="ID"
      />
      <el-table-column
        prop="name"
        label="角色名"
      />

      <el-table-column
        label="操作"
      >
        <template v-if="scope.row.id != 1" slot-scope="scope">
          <el-button type="text" @click="addRoot(scope.row.id)">分配权限</el-button>
          <el-button type="text" @click="add(scope.row)">编辑</el-button>
          <el-button type="text" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="text-align:center;margin-top:20px"
      background
      layout="prev,pager,next,sizes,total,jumper"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="keyWord.pageCount"
      :total="total"
      :current-page="keyWord.page"
      @current-change="currentChange"
      @size-change="sizeChange"
    />
    <el-dialog
      title="分配节点权限"
      :visible.sync="dialogRole"
      width="30%"
    >
      <div>
        <el-tree
          ref="tree"
          :data="menuList"
          :props="defaultProps"
          show-checkbox
          node-key="id"
        />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogRole = false">取 消</el-button>
        <el-button @click="getCheckedKeys">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="操作角色"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="角色名">
            <el-input v-model="form.name" placeholder="请输入角色名" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button @click="sub()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { RoleList, RoleEdit, RoleDel, getMenu, RoleMenu, RoleMenuAuth } from '@/api/user'

export default {
  name: 'User',
  data() {
    return {
      defaultProps: {
        children: 'child',
        label: 'name'
      },
      tableData: [],
      loading: true,
      dialogVisible: false,
      dialogRole: false,
      total: 0,
      keyWord: {
        page: 1,
        pageCount: 10,
        keyWord: ''
      },
      form: {
        id: '',
        name: ''
      },
      oldForm: {
        id: '',
        name: ''
      },
      roleList: [],
      menuList: [],
      roleRootArr: [],
      roleRootAuthId: 0
    }
  },
  created() {
    this.getList()
    this.getMenuList()
  },
  methods: {
    sizeChange(e) {
      this.keyWord.pageCount = e
      this.keyWord.page = 1
      this.loading = true
      this.getList()
    },
    currentChange(e) {
      this.keyWord.page = e
      this.loading = true
      this.getList()
    },
    async getList() {
      this.loading = true
      this.tableData = []
      RoleList(this.keyWord).then(response => {
        this.loading = false
        this.tableData = response.data || []
        this.total = response.data.total || 0
      }).catch(() => {
        this.loading = false
      })
    },
    timestampToTime(times) {
      var date = new Date(times * 1000) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + '-'
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      var D = date.getDate() + ' '
      var h = date.getHours() + ':'
      var m = date.getMinutes() + ':'
      var s = date.getSeconds()
      return Y + M + D + h + m + s
    },
    add(data) {
      if (data) {
        this.form.id = data.id
        this.form.name = data.name
      } else {
        this.form = this.oldForm
      }
      this.dialogVisible = true
    },
    sub() {
      this.loading = true
      RoleEdit(this.form).then(res => {
        this.dialogVisible = false
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.getList()
      }).catch(() => {
        this.loading = false
      })
    },
    del(id) {
      this.$confirm('此操作将删除该节点和所有子节点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        RoleDel({ id: id }).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        }).catch(() => {
          this.loading = false
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addRoot(id) {
      this.roleRootArr = []
      this.loading = true
      this.roleRootAuthId = id
      RoleMenu({ id: id }).then(response => {
        const { data } = response
        this.dialogRole = true
        for (var i = 0; i < data.length; i++) {
          this.roleRootArr.push(data[i].menu_id)
        }
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys(this.roleRootArr, true)
        })
        this.loading = false
      })
    },
    getMenuList() {
      this.tableData = []
      this.treeData = []
      getMenu().then(response => {
        const { data } = response
        this.menuList = data
      })
    },
    getCheckedKeys() {
      var idArr = this.$refs.tree.getCheckedKeys(true)
      var idArrMid = this.$refs.tree.getHalfCheckedKeys()
      if (idArr.length === 0) {
        this.$message({
          type: 'error',
          message: '请为角色分配权限!'
        })
        return
      }
      this.loading = true
      RoleMenuAuth({ id: this.roleRootAuthId, idArr: idArr.join(','), idArrMid: idArrMid.join(',') }).then(response => {
        this.dialogRole = false
        this.loading = false
        this.$message({
          type: 'success',
          message: '角色权限分配成功!'
        })
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss">

</style>
